const arr = [5, 6, 8, 12, 4, 1, 3]
function bubbleSort (arr) {
  let flag = true
  let len = arr.length
  for (let i = 0; i < len; i++) {
    flag = true
    for (let j = 0; j < len - i - 1; j++) {
      if (arr[j] > arr[j + 1]) {
        flag = false
        let temp = arr[j]
        arr[j] = arr[j + 1]
        arr[j + 1] = temp
      }
    }
    if (flag) break
  }
  return arr
}

let arr1 = bubbleSort(arr)
console.log(arr1)